package com.shiguiwu.springmybatis.spring.imports.deferred;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @description: 延时导入类，说明这个类必须在最后在导入。
 * 可以厉害这个来控制最后的容错，检查一些bean是否存在
 * @author: stone
 * @date: Created by 2021/9/29 20:38
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.imports.deferred
 */
@Import({
        MyDeferredImportSelector.class,
        MyDeferredImportSelector.BConfig.class,
        MyDeferredImportSelector.InnerImportSelector.class
})
public class MyDeferredImportSelector implements DeferredImportSelector {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyDeferredImportSelector.class);

    }

    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        return new String[] {AConfig.class.getName()};
    }

    @Configuration
    public static class AConfig {
        @Bean("A==")
        public String name() {
            System.out.println("=======================> A");
            return "A";
        }
    }

    @Configuration
    public static class BConfig {
        @Bean("B==")
        public String name() {
            System.out.println("=======================> B");
            return "B";
        }
    }

    @Configuration
    public static class CConfig {
        @Bean("C==")
        public String name() {
            System.out.println("=======================> C");
            return "C";
        }
    }

    public static class InnerImportSelector implements ImportSelector {

        @Override
        public String[] selectImports(AnnotationMetadata annotationMetadata) {
            return new String[]{CConfig.class.getName()};
        }
    }
}
